<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Point2D.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Point2D code in Java">
<meta NAME="TITLE" CONTENT="Point2D code in Java">
<meta NAME="KEYWORDS" CONTENT="Point2D,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Point2D.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Point2D.java">Point2D.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Point2D.java</span>
<span class="comment"> *  Execution:    java Point2D x0 y0 N</span>
<span class="comment"> *  Dependencies: StdDraw.java StdRandom.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Immutable point data type for points in the plane.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Arrays</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Comparator</span><span class="symbol">;</span>


<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Point</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class is an immutable data type to encapsulate a</span>
<span class="comment"> *  two-dimensional point with real-value coordinates.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Note: in order to deal with the difference behavior of double and </span>
<span class="comment"> *  Double with respect to -0.0 and +0.0, the Point2D constructor converts</span>
<span class="comment"> *  any coordinates that are -0.0 to +0.0.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, </span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/12oop"</span><span class="keyword">&gt;</span><span class="comment">Section 1.2</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of </span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne. </span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Point2D</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by x-coordinate.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> X_ORDER </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">XOrder</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by y-coordinate.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> Y_ORDER </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">YOrder</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by polar radius.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> R_ORDER </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ROrder</span><span class="symbol">();</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> x</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// x coordinate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// y coordinate</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes a new point (x, y).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the x-coordinate</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the y-coordinate</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either </span><span class="keyword">&lt;tt&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">y</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *    is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.NaN</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.POSITIVE_INFINITY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or</span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.NEGATIVE_INFINITY</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Point2D</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Double</span><span class="symbol">.</span><span class="function">isInfinite</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> Double</span><span class="symbol">.</span><span class="function">isInfinite</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Coordinates must be finite"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Double</span><span class="symbol">.</span><span class="function">isNaN</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> Double</span><span class="symbol">.</span><span class="function">isNaN</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Coordinates cannot be NaN"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// convert -0.0 to +0.0</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">          </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// convert -0.0 to +0.0</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">          </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the x-coordinate.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the x-coordinate</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">x</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the y-coordinate.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the y-coordinate</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">y</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the polar radius of this point.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the polar radius of this point in polar coordiantes: sqrt(x*x + y*y)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">r</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">*</span><span class="normal">x </span><span class="symbol">+</span><span class="normal"> y</span><span class="symbol">*</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the angle of this point in polar coordinates.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the angle (in radians) of this point in polar coordiantes (between -pi/2 and pi/2)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">theta</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">atan2</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the angle between this point and that point.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the angle in radians (between -pi and pi) between this point and that point (0 if equal)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">angleTo</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dx </span><span class="symbol">=</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dy </span><span class="symbol">=</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">-</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">atan2</span><span class="symbol">(</span><span class="normal">dy</span><span class="symbol">,</span><span class="normal"> dx</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if a-&gt;b-&gt;c is a counterclockwise turn.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> a first point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> b second point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> c third point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> { -1, 0, +1 } if a-&gt;b-&gt;c is a { clockwise, collinear; counterclocwise } turn.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">ccw</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> b</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> c</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> area2 </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)*(</span><span class="normal">c</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)*(</span><span class="normal">c</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">area2 </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">area2 </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                </span><span class="keyword">return</span><span class="normal">  </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns twice the signed area of the triangle a-b-c.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> a first point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> b second point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> c third point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> twice the signed area of the triangle a-b-c</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">area2</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> b</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> c</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)*(</span><span class="normal">c</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)*(</span><span class="normal">c</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the Euclidean distance between this point and that point.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> that the other point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the Euclidean distance between this point and that point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">distanceTo</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dx </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dy </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(</span><span class="normal">dx</span><span class="symbol">*</span><span class="normal">dx </span><span class="symbol">+</span><span class="normal"> dy</span><span class="symbol">*</span><span class="normal">dy</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the square of the Euclidean distance between this point and that point.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> that the other point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the square of the Euclidean distance between this point and that point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">distanceSquaredTo</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dx </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> dy </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> dx</span><span class="symbol">*</span><span class="normal">dx </span><span class="symbol">+</span><span class="normal"> dy</span><span class="symbol">*</span><span class="normal">dy</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by y-coordinate, breaking ties by x-coordinate.</span>
<span class="comment">     * Formally, the invoking point (x0, y0) is less than the argument point (x1, y1)</span>
<span class="comment">     * if and only if either y0 &lt; y1 or if y0 = y1 and x0 &lt; x1.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  that the other point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the value </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this string is equal to the argument</span>
<span class="comment">     *         string (precisely when </span><span class="keyword">&lt;tt&gt;</span><span class="comment">equals()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> returns </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">);</span>
<span class="comment">     *         a negative integer if this point is less than the argument</span>
<span class="comment">     *         point; and a positive integer if this point is greater than the</span>
<span class="comment">     *         argument point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compareTo</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">&lt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">&gt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">&lt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">&gt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by polar angle (between 0 and 2pi) with respect to this point.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the comparator</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> </span><span class="function">polarOrder</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">PolarOrder</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by atan2() angle (between -pi and pi) with respect to this point.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the comparator</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> </span><span class="function">atan2Order</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Atan2Order</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compares two points by distance to this point.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the comparator</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Comparator&lt;Point2D&gt;</span><span class="normal"> </span><span class="function">distanceToOrder</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DistanceToOrder</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare points according to their x-coordinate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">XOrder</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">&lt;</span><span class="normal"> q</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">&gt;</span><span class="normal"> q</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare points according to their y-coordinate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">YOrder</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">&lt;</span><span class="normal"> q</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">&gt;</span><span class="normal"> q</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare points according to their polar radius</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">ROrder</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> delta </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">*</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">+</span><span class="normal"> p</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">*</span><span class="normal">p</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">q</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">*</span><span class="normal">q</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">+</span><span class="normal"> q</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">*</span><span class="normal">q</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">delta </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">delta </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal"> </span>
<span class="normal">    </span><span class="comment">// compare other points relative to atan2 angle (bewteen -pi/2 and pi/2) they make with this Point</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Atan2Order</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> q1</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> angle1 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">angleTo</span><span class="symbol">(</span><span class="normal">q1</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> angle2 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">angleTo</span><span class="symbol">(</span><span class="normal">q2</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">angle1 </span><span class="symbol">&lt;</span><span class="normal"> angle2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">angle1 </span><span class="symbol">&gt;</span><span class="normal"> angle2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                      </span><span class="keyword">return</span><span class="normal">  </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare other points relative to polar angle (between 0 and 2pi) they make with this Point</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">PolarOrder</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> q1</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dx1 </span><span class="symbol">=</span><span class="normal"> q1</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dy1 </span><span class="symbol">=</span><span class="normal"> q1</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">-</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dx2 </span><span class="symbol">=</span><span class="normal"> q2</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dy2 </span><span class="symbol">=</span><span class="normal"> q2</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">-</span><span class="normal"> y</span><span class="symbol">;</span>

<span class="normal">            </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">dy1 </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> dy2 </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// q1 above; q2 below</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dy2 </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> dy1 </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// q1 below; q2 above</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dy1 </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> dy2 </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">            </span><span class="comment">// 3-collinear and horizontal</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">dx1 </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> dx2 </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dx2 </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> dx1 </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                          </span><span class="keyword">return</span><span class="normal">  </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="function">ccw</span><span class="symbol">(</span><span class="normal">Point2D</span><span class="symbol">.</span><span class="keyword">this</span><span class="symbol">,</span><span class="normal"> q1</span><span class="symbol">,</span><span class="normal"> q2</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// both above or below</span>

<span class="normal">            </span><span class="comment">// Note: ccw() recomputes dx1, dy1, dx2, and dy2</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare points according to their distance to this point</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">DistanceToOrder</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparator</span><span class="symbol">&lt;</span><span class="normal">Point2D</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Point2D</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dist1 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">distanceSquaredTo</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> dist2 </span><span class="symbol">=</span><span class="normal"> </span><span class="function">distanceSquaredTo</span><span class="symbol">(</span><span class="normal">q</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">dist1 </span><span class="symbol">&lt;</span><span class="normal"> dist2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dist1 </span><span class="symbol">&gt;</span><span class="normal"> dist2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                    </span><span class="keyword">return</span><span class="normal">  </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**       </span>
<span class="comment">     * Compares this point to the specified point.</span>
<span class="comment">     *       </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  other the other point</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this point equals </span><span class="keyword">&lt;tt&gt;</span><span class="comment">other</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">equals</span><span class="symbol">(</span><span class="usertype">Object</span><span class="normal"> other</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other</span><span class="symbol">.</span><span class="function">getClass</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">getClass</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Point2D</span><span class="normal"> that </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Point2D</span><span class="symbol">)</span><span class="normal"> other</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y </span><span class="symbol">==</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Return a string representation of this point.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a string representation of this point in the format (x, y)</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="string">"("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> x </span><span class="symbol">+</span><span class="normal"> </span><span class="string">", "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> y </span><span class="symbol">+</span><span class="normal"> </span><span class="string">")"</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns an integer hash code for this point.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> an integer hash code for this point</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">hashCode</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hashX </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">Double</span><span class="symbol">)</span><span class="normal"> x</span><span class="symbol">).</span><span class="function">hashCode</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hashY </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">Double</span><span class="symbol">)</span><span class="normal"> y</span><span class="symbol">).</span><span class="function">hashCode</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="number">31</span><span class="symbol">*</span><span class="normal">hashX </span><span class="symbol">+</span><span class="normal"> hashY</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Plot this point using standard draw.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">draw</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">point</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Plot a line from this point to that point using standard draw.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> that the other point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">drawTo</span><span class="symbol">(</span><span class="usertype">Point2D</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">line</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">,</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the point data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> x0 </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> y0 </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">]);</span>

<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setCanvasSize</span><span class="symbol">(</span><span class="number">800</span><span class="symbol">,</span><span class="normal"> </span><span class="number">800</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setXscale</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">100</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setYscale</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">100</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">(.</span><span class="number">005</span><span class="symbol">);</span>
<span class="normal">        Point2D</span><span class="symbol">[]</span><span class="normal"> points </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Point2D</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="number">100</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="number">100</span><span class="symbol">);</span>
<span class="normal">            points</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Point2D</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">            points</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// draw p = (x0, x1) in red</span>
<span class="normal">        </span><span class="usertype">Point2D</span><span class="normal"> p </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Point2D</span><span class="symbol">(</span><span class="normal">x0</span><span class="symbol">,</span><span class="normal"> y0</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">RED</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">(.</span><span class="number">02</span><span class="symbol">);</span>
<span class="normal">        p</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">();</span>


<span class="normal">        </span><span class="comment">// draw line segments from p to each point, one at a time, in polar order</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">();</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">BLUE</span><span class="symbol">);</span>
<span class="normal">        Arrays</span><span class="symbol">.</span><span class="function">sort</span><span class="symbol">(</span><span class="normal">points</span><span class="symbol">,</span><span class="normal"> p</span><span class="symbol">.</span><span class="function">polarOrder</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            p</span><span class="symbol">.</span><span class="function">drawTo</span><span class="symbol">(</span><span class="normal">points</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">            StdDraw</span><span class="symbol">.</span><span class="function">show</span><span class="symbol">(</span><span class="number">100</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
